﻿' ******************************************************************************
' ** 
' **  Yahoo! Managed
' **  Written by Marius Häusler 2011
' **  It would be pleasant, if you contact me when you are using this code.
' **  Contact: YahooFinanceManaged@gmail.com
' **  Project Home: http://code.google.com/p/yahoo-finance-managed/
' **  
' ******************************************************************************
' **  
' **  Copyright 2011 Marius Häusler
' **  
' **  Licensed under the Apache License, Version 2.0 (the "License");
' **  you may not use this file except in compliance with the License.
' **  You may obtain a copy of the License at
' **  
' **    http://www.apache.org/licenses/LICENSE-2.0
' **  
' **  Unless required by applicable law or agreed to in writing, software
' **  distributed under the License is distributed on an "AS IS" BASIS,
' **  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' **  See the License for the specific language governing permissions and
' **  limitations under the License.
' ** 
' ******************************************************************************


Namespace YahooManaged.Finance.ImportExport

    ''' <summary>
    ''' Class for parsing managed data to and from XML
    ''' </summary>
    ''' <remarks></remarks>
    Public Class XML
        Private ReadOnly mHelper As New MyHelper
        Private ReadOnly mFinanceHelper As New FinanceHelper

        ''' <summary>
        ''' Writes a QuoteBaseData to XML format
        ''' </summary>
        ''' <param name="writer">The used XML writer</param>
        ''' <param name="quote">The used QuoteBaseData</param>
        ''' <param name="culture">The used culture for formating dates and numbers. If parameter value is null/Nothing, default Culture will be used.</param>
        ''' <remarks></remarks>
        Public Sub FromQuoteBaseData(ByVal writer As XmlTextWriter, ByVal quote As QuoteBaseData, Optional ByVal culture As Globalization.CultureInfo = Nothing)
            Dim ci As Globalization.CultureInfo = Globalization.CultureInfo.CurrentCulture
            If culture IsNot Nothing Then ci = culture
            With writer
                .WriteStartElement("Quote")

                If quote.ID <> String.Empty Then .WriteAttributeString("ID", quote.ID)

                .WriteStartElement(FinanceHelper.NameQuoteBaseID)
                .WriteValue(quote.ID)
                .WriteEndElement()

                .WriteStartElement(FinanceHelper.NameQuoteBaseLastTradePriceOnly)
                .WriteValue(mHelper.ObjectToString(quote.LastTradePriceOnly, ci))
                .WriteEndElement()

                .WriteStartElement(FinanceHelper.NameQuoteBaseChange)
                .WriteValue(mHelper.ObjectToString(quote.Change, ci))
                .WriteEndElement()

                .WriteStartElement(FinanceHelper.NameQuoteBaseOpen)
                .WriteValue(mHelper.ObjectToString(quote.Open, ci))
                .WriteEndElement()

                .WriteStartElement(FinanceHelper.NameQuoteBaseDaysHigh)
                .WriteValue(mHelper.ObjectToString(quote.DaysHigh, ci))
                .WriteEndElement()

                .WriteStartElement(FinanceHelper.NameQuoteBaseDaysLow)
                .WriteValue(mHelper.ObjectToString(quote.DaysLow, ci))
                .WriteEndElement()

                .WriteStartElement(FinanceHelper.NameQuoteBaseVolume)
                .WriteValue(mHelper.ObjectToString(quote.Volume, ci))
                .WriteEndElement()

                .WriteStartElement(FinanceHelper.NameQuoteBaseLastTradeDate)
                .WriteValue(quote.LastTradeDate.ToShortDateString)
                .WriteEndElement()

                .WriteStartElement(FinanceHelper.NameQuoteBaseLastTradeTime)
                .WriteValue(quote.LastTradeTime.ToShortTimeString)
                .WriteEndElement()

                .WriteEndElement()
            End With
        End Sub
        ''' <summary>
        ''' Tries to read QuoteBaseData from XML
        ''' </summary>
        ''' <param name="quote">The XML node of a QuotesBaseData</param>
        ''' <param name="culture">The used culture for formating dates and numbers. If parameter value is null/Nothing, default Culture will be used.</param>
        ''' <returns>The converted basic quote data or Nothing</returns>
        ''' <remarks></remarks>
        Public Function ToQuoteBaseData(ByVal quote As XmlNode, Optional ByVal culture As Globalization.CultureInfo = Nothing) As QuoteBaseData
            If quote IsNot Nothing AndAlso quote.Name.ToLower = "quote" Then
                Dim ci As Globalization.CultureInfo = Globalization.CultureInfo.CurrentCulture
                If culture IsNot Nothing Then ci = culture
                Dim q As New QuoteBaseData
                For Each propertyNode As XmlNode In quote.ChildNodes
                    Select Case propertyNode.Name
                        Case FinanceHelper.NameQuoteBaseID : q.SetID(propertyNode.InnerText)
                        Case FinanceHelper.NameQuoteBaseLastTradePriceOnly : Double.TryParse(propertyNode.InnerText, Globalization.NumberStyles.Currency, ci, q.LastTradePriceOnly)
                        Case FinanceHelper.NameQuoteBaseLastTradeDate : Date.TryParse(propertyNode.InnerText, ci, Globalization.DateTimeStyles.AdjustToUniversal, q.LastTradeDate)
                        Case FinanceHelper.NameQuoteBaseLastTradeTime : Date.TryParse(propertyNode.InnerText, ci, Globalization.DateTimeStyles.AdjustToUniversal, q.LastTradeTime)
                        Case FinanceHelper.NameQuoteBaseChange : Double.TryParse(propertyNode.InnerText, Globalization.NumberStyles.Currency, ci, q.Change)
                        Case FinanceHelper.NameQuoteBaseOpen : Double.TryParse(propertyNode.InnerText, Globalization.NumberStyles.Currency, ci, q.Open)
                        Case FinanceHelper.NameQuoteBaseDaysHigh : Double.TryParse(propertyNode.InnerText, Globalization.NumberStyles.Currency, ci, q.DaysHigh)
                        Case FinanceHelper.NameQuoteBaseDaysLow : Double.TryParse(propertyNode.InnerText, Globalization.NumberStyles.Currency, ci, q.DaysLow)
                        Case FinanceHelper.NameQuoteBaseVolume : Long.TryParse(propertyNode.InnerText, Globalization.NumberStyles.Integer, ci, q.Volume)
                    End Select
                Next
                Return q
            Else
                Return Nothing
            End If
        End Function

        ''' <summary>
        ''' Writes a QuoteData to XML format
        ''' </summary>
        ''' <param name="writer">The used XML writer</param>
        ''' <param name="quote">The used QuoteData</param>
        ''' <param name="culture">The used culture for formating dates and numbers. If parameter value is null/Nothing, default Culture will be used.</param>
        ''' <remarks></remarks>
        Public Sub FromQuoteData(ByVal writer As XmlTextWriter, ByVal quote As QuoteData, Optional ByVal culture As Globalization.CultureInfo = Nothing)
            Me.FromQuoteData(writer, quote, Nothing, culture)
        End Sub
        ''' <summary>
        ''' Writes a QuoteData to XML format
        ''' </summary>
        ''' <param name="writer">The used XML writer</param>
        ''' <param name="quote">The used QuoteData</param>
        ''' <param name="properties">The used properties of the quotes</param>
        ''' <param name="culture">The used culture for formating dates and numbers. If parameter value is null/Nothing, default Culture will be used.</param>
        ''' <remarks></remarks>
        Public Sub FromQuoteData(ByVal writer As XmlTextWriter, ByVal quote As QuoteData, ByVal properties As IEnumerable(Of QuoteProperty), Optional ByVal culture As Globalization.CultureInfo = Nothing)
            Dim ci As Globalization.CultureInfo = Globalization.CultureInfo.CurrentCulture
            If culture IsNot Nothing Then ci = culture
            With writer
                .WriteStartElement("Quote")
                If quote.Values(QuoteProperty.Symbol) IsNot Nothing Then .WriteAttributeString("ID", quote.Values(QuoteProperty.Symbol).ToString())
                Dim prps() As QuoteProperty = mFinanceHelper.CheckPropertiesOfQuotesData(New QuoteData() {quote}, properties)
                For Each qp As QuoteProperty In prps
                    .WriteStartElement(qp.ToString)
                    .WriteValue(mHelper.ObjectToString(quote(qp), ci))
                    .WriteEndElement()
                Next
                .WriteEndElement()
            End With
        End Sub
        ''' <summary>
        ''' Tries to read a QuoteData from XML
        ''' </summary>
        ''' <param name="node">The XML node of a QuoteData</param>
        ''' <param name="culture">The used culture for formating dates and numbers. If parameter value is null/Nothing, default Culture will be used.</param>
        ''' <returns>The converted quote data or Nothing</returns>
        ''' <remarks></remarks>
        Public Function ToQuoteData(ByVal node As XmlNode, Optional ByVal culture As Globalization.CultureInfo = Nothing) As QuoteData
            If node IsNot Nothing AndAlso node.Name.ToLower = "quote" Then
                Dim ci As Globalization.CultureInfo = Globalization.CultureInfo.CurrentCulture
                If culture IsNot Nothing Then ci = culture
                Dim quote As New QuoteData
                For Each propertyNode As XmlNode In node.ChildNodes
                    For Each qp As QuoteProperty In [Enum].GetValues(GetType(QuoteProperty))
                        If propertyNode.Name = qp.ToString Then
                            quote(qp) = mHelper.StringToObject(propertyNode.InnerText, ci)
                            Exit For
                        End If
                    Next
                Next
                Return quote
            Else
                Return Nothing
            End If
        End Function

        ''' <summary>
        ''' Writes a QuoteOption to XML format
        ''' </summary>
        ''' <param name="writer">The used XML writer</param>
        ''' <param name="quoteOption">The used QuoteOption</param>
        ''' <param name="culture">The used culture for formating dates and numbers. If parameter value is null/Nothing, default Culture will be used.</param>
        ''' <remarks></remarks>
        Public Sub FromQuoteOption(ByVal writer As XmlTextWriter, ByVal quoteOption As QuoteOptionData, Optional ByVal culture As Globalization.CultureInfo = Nothing)
            Dim ci As Globalization.CultureInfo = Globalization.CultureInfo.CurrentCulture
            If culture IsNot Nothing Then ci = culture
            With writer
                .WriteStartElement("Option")

                .WriteAttributeString(FinanceHelper.NameOptionSymbol, quoteOption.Symbol)
                .WriteAttributeString(FinanceHelper.NameOptionType, IIf(quoteOption.Type = QuoteOptionType.Call, "C", "P").ToString)

                .WriteStartElement(FinanceHelper.NameOptionLastPrice)
                .WriteValue(mHelper.ObjectToString(quoteOption.LastPrice, ci))
                .WriteEndElement()

                .WriteStartElement(FinanceHelper.NameOptionStrikePrice)
                .WriteValue(mHelper.ObjectToString(quoteOption.StrikePrice, ci))
                .WriteEndElement()

                .WriteStartElement(FinanceHelper.NameOptionChange)
                .WriteValue(mHelper.ObjectToString(Math.Abs(quoteOption.Change), ci))
                .WriteEndElement()

                .WriteStartElement(FinanceHelper.NameOptionChangeDir)
                .WriteValue(IIf(quoteOption.Change >= 0, "Up", "Down").ToString)
                .WriteEndElement()

                .WriteStartElement(FinanceHelper.NameOptionBid)
                .WriteValue(mHelper.ObjectToString(quoteOption.Bid, ci))
                .WriteEndElement()

                .WriteStartElement(FinanceHelper.NameOptionAsk)
                .WriteValue(mHelper.ObjectToString(quoteOption.Ask, ci))
                .WriteEndElement()

                .WriteStartElement(FinanceHelper.NameOptionVolume)
                .WriteValue(mHelper.ObjectToString(quoteOption.Volume, ci))
                .WriteEndElement()

                .WriteStartElement(FinanceHelper.NameOptionOpenInterest)
                .WriteValue(mHelper.ObjectToString(quoteOption.OpenInterest, ci))
                .WriteEndElement()

                .WriteEndElement()
            End With
        End Sub
        ''' <summary>
        ''' Tries to read a QuoteOption from XML
        ''' </summary>
        ''' <param name="node">The XML node of QuoteOption</param>
        ''' <param name="culture">The used culture for formating dates and numbers. If parameter value is null/Nothing, default Culture will be used.</param>
        ''' <returns>The converted quote data or Nothing</returns>
        ''' <remarks></remarks>
        Public Function ToQuoteOption(ByVal node As XmlNode, Optional ByVal culture As Globalization.CultureInfo = Nothing) As QuoteOptionData
            If node IsNot Nothing AndAlso node.Name.ToLower = "option" Then
                Dim ci As Globalization.CultureInfo = Globalization.CultureInfo.CurrentCulture
                If culture IsNot Nothing Then ci = culture
                Dim symbol As String = String.Empty
                Dim t As String = String.Empty
                For Each att As XmlAttribute In node.Attributes
                    Select Case att.Name
                        Case FinanceHelper.NameOptionSymbol : symbol = att.Value
                        Case "type" : t = att.Value
                    End Select
                Next
                Dim type As QuoteOptionType = QuoteOptionType.Call
                If t.ToLower = "p" Then type = QuoteOptionType.Put
                Dim strikePrice, lastPrice, change, bid, ask As Double
                Dim volume, openInt As Integer
                For Each propertyNode As XmlNode In node.ChildNodes
                    Select Case propertyNode.Name
                        Case FinanceHelper.NameOptionStrikePrice : Double.TryParse(propertyNode.InnerText, Globalization.NumberStyles.Currency, ci, strikePrice)
                        Case FinanceHelper.NameOptionLastPrice : Double.TryParse(propertyNode.InnerText, Globalization.NumberStyles.Currency, ci, lastPrice)
                        Case FinanceHelper.NameOptionChange : Double.TryParse(propertyNode.InnerText, Globalization.NumberStyles.Currency, ci, change)
                        Case FinanceHelper.NameOptionChangeDir : If propertyNode.InnerText = "Down" Then change *= -1
                        Case FinanceHelper.NameOptionBid : Double.TryParse(propertyNode.InnerText, Globalization.NumberStyles.Currency, ci, bid)
                        Case FinanceHelper.NameOptionAsk : Double.TryParse(propertyNode.InnerText, Globalization.NumberStyles.Currency, ci, ask)
                        Case FinanceHelper.NameOptionVolume : Integer.TryParse(propertyNode.InnerText, Globalization.NumberStyles.Integer, ci, volume)
                        Case FinanceHelper.NameOptionOpenInterest : Integer.TryParse(propertyNode.InnerText, Globalization.NumberStyles.Integer, ci, openInt)
                    End Select
                Next
                Return New QuoteOptionData(symbol, type, strikePrice, lastPrice, change, bid, ask, volume, openInt)
            Else
                Return Nothing
            End If
        End Function

        ''' <summary>
        ''' Writes HistQuoteData to XML format
        ''' </summary>
        ''' <param name="writer">The used XML writer</param>
        ''' <param name="quote">The used HistQuoteData</param>
        ''' <param name="culture">The used culture for formating dates and numbers. If parameter value is null/Nothing, default Culture will be used.</param>
        ''' <remarks>Creates a main node for all periods</remarks>
        Public Sub FromHistQuoteData(ByVal writer As XmlTextWriter, ByVal quote As HistQuoteData, Optional ByVal culture As Globalization.CultureInfo = Nothing)
            Dim ci As Globalization.CultureInfo = Globalization.CultureInfo.CurrentCulture
            If culture IsNot Nothing Then ci = culture
            With writer
                .WriteStartElement("HistQuote")

                .WriteStartElement(FinanceHelper.NameHistQuoteDate)
                .WriteValue(quote.TradingDate.ToShortDateString)
                .WriteEndElement()

                .WriteStartElement(FinanceHelper.NameHistQuoteOpen)
                .WriteValue(mHelper.ObjectToString(quote.Open, ci))
                .WriteEndElement()

                .WriteStartElement(FinanceHelper.NameHistQuoteHigh)
                .WriteValue(mHelper.ObjectToString(quote.High, ci))
                .WriteEndElement()

                .WriteStartElement(FinanceHelper.NameHistQuoteLow)
                .WriteValue(mHelper.ObjectToString(quote.Low, ci))
                .WriteEndElement()

                .WriteStartElement(FinanceHelper.NameHistQuoteClose)
                .WriteValue(mHelper.ObjectToString(quote.Close, ci))
                .WriteEndElement()

                .WriteStartElement(FinanceHelper.NameHistQuoteVolume)
                .WriteValue(mHelper.ObjectToString(quote.Volume, ci))
                .WriteEndElement()

                .WriteStartElement(FinanceHelper.NameHistQuoteAdjClose)
                .WriteValue(mHelper.ObjectToString(quote.CloseAdjusted, ci))
                .WriteEndElement()

                .WriteEndElement()
            End With
        End Sub
        ''' <summary>
        ''' Tries to read a HistQuoteData from XML
        ''' </summary>
        ''' <param name="node">The XML node of HistQuoteData</param>
        ''' <param name="culture">The used culture for formating dates and numbers. If parameter value is null/Nothing, default Culture will be used.</param>
        ''' <returns>The converted historic quote data or Nothing</returns>
        ''' <remarks></remarks>
        Public Function ToHistQuoteData(ByVal node As XmlNode, Optional ByVal culture As Globalization.CultureInfo = Nothing) As HistQuoteData
            If node IsNot Nothing AndAlso node.Name.ToLower = "histquote" AndAlso node.ChildNodes.Count > 0 Then
                Dim ci As Globalization.CultureInfo = Globalization.CultureInfo.CurrentCulture
                If culture IsNot Nothing Then ci = culture
                Dim qd As New HistQuoteData
                For Each cNode As XmlNode In node.ChildNodes
                    Select Case cNode.Name
                        Case FinanceHelper.NameHistQuoteDate : Date.TryParse(node.ChildNodes(0).InnerText, qd.TradingDate)
                        Case FinanceHelper.NameHistQuoteOpen : Double.TryParse(node.ChildNodes(1).InnerText, Globalization.NumberStyles.Currency, ci, qd.Open)
                        Case FinanceHelper.NameHistQuoteHigh : Double.TryParse(node.ChildNodes(2).InnerText, Globalization.NumberStyles.Currency, ci, qd.High)
                        Case FinanceHelper.NameHistQuoteLow : Double.TryParse(node.ChildNodes(3).InnerText, Globalization.NumberStyles.Currency, ci, qd.Low)
                        Case FinanceHelper.NameHistQuoteClose : Double.TryParse(node.ChildNodes(4).InnerText, Globalization.NumberStyles.Currency, ci, qd.Close)
                        Case FinanceHelper.NameHistQuoteVolume : Long.TryParse(node.ChildNodes(5).InnerText, Globalization.NumberStyles.Integer, ci, qd.Volume)
                        Case FinanceHelper.NameHistQuoteAdjClose : Double.TryParse(node.ChildNodes(6).InnerText, Globalization.NumberStyles.Currency, ci, qd.CloseAdjusted)
                    End Select
                Next
                Return qd
            Else
                Return Nothing
            End If
        End Function

        ''' <summary>
        ''' Writes an ExchangeRateData to XML format
        ''' </summary>
        ''' <param name="writer">The used XML writer</param>
        ''' <param name="rateData">The used ExchangeRateData</param>
        ''' <param name="culture">The used culture for formating dates and numbers. If parameter value is null/Nothing, default Culture will be used.</param>
        ''' <remarks></remarks>
        Public Sub FromExchangeRateData(ByVal writer As XmlTextWriter, ByVal rateData As Support.ExchangeRateData, Optional ByVal culture As Globalization.CultureInfo = Nothing)
            If writer IsNot Nothing Then
                With writer
                    .WriteStartElement("ExchangeRate")

                    .WriteStartElement("Symbol")
                    .WriteValue(rateData.CurrencyRelation.ID)
                    .WriteEndElement()

                    .WriteStartElement("BaseCurrency")
                    .WriteAttributeString("Name", rateData.CurrencyRelation.BaseCurrency.Description)
                    .WriteValue(rateData.CurrencyRelation.BaseCurrency.ID.ToString)
                    .WriteEndElement()

                    .WriteStartElement("DepCurrency")
                    .WriteAttributeString("Name", rateData.CurrencyRelation.DepCurrency.Description)
                    .WriteValue(rateData.CurrencyRelation.DepCurrency.ID.ToString)
                    .WriteEndElement()

                    FromQuoteBaseData(writer, rateData, culture)

                    .WriteEndElement()
                End With
            End If
        End Sub
        ''' <summary>
        ''' Tries to read an ExchangeRateData from XML
        ''' </summary>
        ''' <param name="node">The XML node of ExchangeRateData</param>
        ''' <param name="culture">The used culture for formating dates and numbers. If parameter value is null/Nothing, default Culture will be used.</param>
        ''' <returns>The converted exchange item or Nothing</returns>
        ''' <remarks></remarks>
        Public Function ToExchangeRateData(ByVal node As XmlNode, Optional ByVal culture As Globalization.CultureInfo = Nothing) As Support.ExchangeRateData
            If node IsNot Nothing AndAlso node.Name.ToLower = "exchangerate" Then
                Dim ci As Globalization.CultureInfo = Globalization.CultureInfo.CurrentCulture
                If culture IsNot Nothing Then ci = culture
                Dim base As CurrencyInfo = Nothing
                Dim dep As CurrencyInfo = Nothing
                Dim qData As New QuoteBaseData
                For Each cNode As XmlNode In node.ChildNodes
                    Select Case cNode.Name.ToLower
                        Case "basecurrency"
                            Dim desc As String = String.Empty
                            Dim att = cNode.Attributes("Name")
                            If att IsNot Nothing Then desc = att.Value
                            base = New CurrencyInfo(cNode.InnerText, desc)

                        Case "depcurrency"
                            Dim desc As String = String.Empty
                            Dim att = cNode.Attributes("Name")
                            If att IsNot Nothing Then desc = att.Value
                            dep = New CurrencyInfo(cNode.InnerText, desc)
                        Case "quote"
                            qData = Me.ToQuoteBaseData(cNode, ci)
                    End Select
                Next
                If base IsNot Nothing And dep IsNot Nothing Then
                    Return New Support.ExchangeRateData(base, dep, qData)
                Else
                    Return Nothing
                End If
            End If
            Return Nothing
        End Function

        Public Function ToSector(ByVal sector As XmlNode, Optional ByVal culture As Globalization.CultureInfo = Nothing) As SectorData
            If sector IsNot Nothing AndAlso sector.Name.ToLower = "sector" Then
                Dim ci As Globalization.CultureInfo = Globalization.CultureInfo.CurrentCulture
                If culture IsNot Nothing Then ci = culture
                Dim sect As New SectorData
                Dim nameAtt = sector.Attributes(FinanceHelper.NameMarketName)
                If nameAtt IsNot Nothing Then
                    For s As Sector = 0 To Finance.Sector.Utilities
                        If s.ToString.Replace("_", " ") = nameAtt.Value Then
                            sect.ID = s
                            Exit For
                        End If
                    Next
                End If
                For Each industryNode As XmlNode In sector.ChildNodes
                    If industryNode.Name = "industry" Then
                        Dim ind As New IndustryData
                        For Each att As XmlAttribute In industryNode.Attributes
                            If att.Name = FinanceHelper.NameIndustryID Then
                                Dim i As Integer = 0
                                Integer.TryParse(att.Value, i)
                                If i <> 0 Then
                                    ind.ID = CType(i, Industry)
                                End If
                            ElseIf att.Name = FinanceHelper.NameMarketName Then : ind.Name = att.Value
                            End If
                        Next
                        sect.Industries.Add(ind)
                    End If
                Next
                Return sect
            Else
                Return Nothing
            End If
        End Function
        Public Function ToIndustry(ByVal industry As XmlNode, Optional ByVal culture As Globalization.CultureInfo = Nothing) As IndustryData
            If industry.Name = "industry" Then
                Dim ind As New IndustryData
                For Each att As XmlAttribute In industry.Attributes
                    If att.Name = FinanceHelper.NameIndustryID Then
                        Dim i As Integer = 0
                        Integer.TryParse(att.Value, i)
                        If i <> 0 Then
                            ind.ID = CType(i, Industry)
                        End If
                    ElseIf att.Name = FinanceHelper.NameMarketName Then : ind.Name = att.Value
                    End If
                Next
                For Each companyNode As XmlNode In industry.ChildNodes
                    If companyNode.Name = "company" Then
                        Dim comp As New CompanyInfoData
                        For Each att As XmlAttribute In companyNode.Attributes
                            If att.Name = FinanceHelper.NameCompanySymbol Then : comp.SetID(att.Value)
                            ElseIf att.Name = FinanceHelper.NameMarketName Then : comp.Name = att.Value
                            End If
                        Next
                        ind.Companies.Add(comp)
                    End If
                Next
                Return ind
            Else
                Return Nothing
            End If
        End Function
        Public Function ToCompanyData(ByVal stock As XmlNode, Optional ByVal culture As Globalization.CultureInfo = Nothing) As CompanyInfoData
            If stock.Name = "stock" Then
                Dim ci As Globalization.CultureInfo = Globalization.CultureInfo.CurrentCulture
                If culture IsNot Nothing Then ci = culture
                Dim stk As New CompanyInfoData
                For Each att As XmlAttribute In stock.Attributes
                    If att.Name = FinanceHelper.NameCompanySymbol Then stk.SetID(att.Value.ToUpper)
                Next
                For Each propertyNode As XmlNode In stock.ChildNodes
                    Select Case propertyNode.Name
                        Case FinanceHelper.NameCompanyCompanyName : stk.Name = propertyNode.InnerText
                        Case FinanceHelper.NameCompanyStart
                            Dim dte As Date
                            If Date.TryParse(propertyNode.InnerText.Replace(FinanceHelper.NameCompanyNotAvailable, "1"), ci, Globalization.DateTimeStyles.AssumeUniversal, dte) Then stk.StartDate = dte
                        Case FinanceHelper.NameCompanyEnd
                            Dim dte As Date
                            Dim dateStr As String = String.Empty
                            If propertyNode.InnerText.IndexOf(FinanceHelper.NameCompanyNotAvailable) > -1 Then
                                Dim dates As String() = propertyNode.InnerText.Split("-"c)
                                If dates.Length >= 3 Then
                                    If dates(0).IndexOf(FinanceHelper.NameCompanyNotAvailable) > -1 Then : dateStr &= Date.Now.Year.ToString & "-"
                                    Else : dateStr &= dates(0) & "-"
                                    End If
                                    If dates(1).IndexOf(FinanceHelper.NameCompanyNotAvailable) > -1 Then : dateStr &= Date.Now.Month.ToString & "-"
                                    Else : dateStr &= dates(1) & "-"
                                    End If
                                    If dates(2).IndexOf(FinanceHelper.NameCompanyNotAvailable) > -1 Then : dateStr &= Date.Now.Day.ToString & "-"
                                    Else : dateStr &= dates(2)
                                    End If
                                    If dates.Length > 3 Then
                                        dateStr &= "-"
                                        For i As Integer = 3 To dates.Length - 1
                                            dateStr &= dates(i) & "-"
                                        Next
                                    End If
                                Else
                                    dateStr = propertyNode.InnerText.Replace(FinanceHelper.NameCompanyNotAvailable, Date.Now.Month.ToString)
                                End If
                            End If
                            If Date.TryParse(dateStr, ci, Globalization.DateTimeStyles.AssumeUniversal, dte) Then stk.EndDate = dte
                        Case FinanceHelper.NameCompanySector : stk.SectorName = propertyNode.InnerText
                        Case FinanceHelper.NameCompanyIndustry : stk.IndustryName = propertyNode.InnerText
                        Case FinanceHelper.NameCompanyFullTimeEmployees
                            Dim i As Integer
                            If Integer.TryParse(propertyNode.InnerText, Globalization.NumberStyles.Any, ci, i) Then stk.FullTimeEmployees = i
                    End Select
                Next
                Return stk
            Else
                Return Nothing
            End If
        End Function

    End Class

End Namespace